
/**   
 * @Title: BaseController.java 
 * @Package: com.aft.elh.module.sys.controller 
 * @author Mandarava  
 * @date 2016��1��21�� ����9:38:57 
 * @version 1.3.1 
 * @Email wuyan1688@qq.com
 */

package com.aft.terra.modules.basic;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;

import com.aft.terra.app.Constants;
import com.aft.terra.common.utils.RedisKit;
import com.aft.terra.modules.game.model.ShareUser;
import com.aft.terra.modules.game.model.User;
import com.aft.terra.modules.member.model.Wechat;
import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.redis.Cache;

/**
 * ����controler
 * 
 * @author Mandarava
 * @date 2016��1��21�� ����9:57:58
 * @version V1.3.1
 * @param <M>
 */

public class BaseController<M extends Model<M>> extends Controller {

	private Class<M> modelClass;

	public BaseController() {
		this.modelClass = getClazz();
	}

	@SuppressWarnings("unchecked")
	public Class<M> getClazz() {
		Type t = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) t).getActualTypeArguments();
		return (Class<M>) params[0];
	}

	/**
	 * ��ȡModel����
	 * 
	 * @author Mandarava
	 * @param modelName
	 * @param num
	 * @return
	 */
	public List<M> getModelList(String modelName, Integer num) {
		return getModelList(getModelClass(), modelName, num);
	}

	/**
	 * ��ȡModel����
	 * 
	 * @author Mandarava
	 * @param modelClass
	 * @param modelName
	 * @param num
	 * @return
	 */
	public <T> List<T> getModelList(Class<T> modelClass, String modelName, Integer num) {
		List<T> list = new ArrayList<T>();
		for (int i = 0; i < num; i++) {
			list.add(getModel(modelClass, modelName + "[" + i + "]"));
		}
		return list;
	}

	/**
	 * ��ȡ��������
	 * 
	 * @author Mandarava
	 * @param paraName
	 * @param num
	 * @return
	 */
	public List<String> getParalList(String paraName, Integer num) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < num; i++) {
			list.add(getPara(paraName + "[" + i + "]"));
		}
		return list;
	}

	/**
	 * ������Ϣ����תָ��ҳ��
	 * 
	 * @author Mandarava
	 * @param message
	 * @param url
	 */
	public void renderMessage(String message, String url) {
		setAttr("message", message);
		render(url);
	}

	public Class<M> getModelClass() {
		return modelClass;
	}

	public void setModelClass(Class<M> modelClass) {
		this.modelClass = modelClass;
	}

	/**
	 * 授权登陆成功后，处理用户信息共享 存在cookie中的为当前登陆用户的id redis中的key为用户的id，value为用户对象
	 * 
	 * @author Mandarava
	 * @param user
	 */
	public String shareSession(Wechat wechat,String state) {
		Cache cache = RedisKit.getCache();
		User user = User.dao.queryWechatUser(wechat.getId());
		if (user == null) {
			user = new User();
			user.setCreateDate(new Date());
			user.setUserName(wechat.getWNickname());
			user.setWechatId(wechat.getId());
			user.save();
			LogKit.info(state+" ---------------------------------------state");
			if(StrKit.notBlank(state)){
				ShareUser r = new ShareUser();
				r.setCreateDate(new Date());
				r.setShareUserId(Long.valueOf(state));
				r.setUserId(user.getId());
				r.setType("0");
				r.save();
			}
		}
		Cookie cookie = new Cookie(Constants.SESSION_NAME, user.getId() + "");
		cookie.setPath("/");
		cookie.setMaxAge(Constants.DEFAULT_COOKIE_TIME_LIMIT);
		cache.setex(user.getId(), Constants.DEFAULT_COOKIE_TIME_LIMIT, user);
		setAttr(Constants.SESSION_NAME, user);
		setCookie(cookie);
		return user.getId() + "";
	}

}
